var LoadHeadCount = 0;
/**
 * 加载头像
 * @class
 * @extend cc.Sprite
 */
var LoadHead = cc.Sprite.extend({
    headUrl: "",
    m_Width: 0,
    m_Height: 0,
    m_headID: 0,
    ctor: function (headID) {
        if (headID <= 0)
            headID = 1;

        this.m_headID = headID;
        this._super();

        if (headID <= 800) {
            var str = dzmjNamespace.GetZero(headID, 3);
            var imageName = "zjm_qqtx_" + str + "u.png";

            cc.log(imageName);

            var spriteFrame = cc.spriteFrameCache.getSpriteFrame(imageName);
            this.initWithSpriteFrame(spriteFrame);
        }
    },
    onExit: function () {
        this._super();
        this.setHeadTexture = null;
    },
    loadImg: function () {
        if (this.m_headID < 801)
            return;

        var self = this;

        if (self.loadComplete) {
            self.loadComplete();
        }
    },

    removeZhao: function (zhao) {
        if (zhao != undefined && zhao != null && dzmjNamespace.isObjectValid(zhao))
            zhao.removeFromParent();
    },

    loadGood: function (goodIconID, zhao) {
        if (goodIconID != null && goodIconID >= 5000 && goodIconID < 5062) {
            var goodIconName = "sc_swjp_" + this.getIconName(goodIconID, 4999) + "u.png";

            var spriteFrame = cc.spriteFrameCache.getSpriteFrame(goodIconName);
            this.initWithSpriteFrame(spriteFrame);

            this.removeZhao(zhao);
        }

        ////测试服
        //var url = "http://219.83.160.99:80/mrsk/getUrl?id=" + this.m_headID;

        //正式服
        var url = "http://otstatic.daofenggame.com/sfqq/upLoad/goods/" + this.m_headID + ".png";

        var that = this;

        cc.log(url);

        cc.loader.loadImg(url, {width: 100, height: 100}, function (err, texture) {
            if (err) {
                cc.log(err);
                that.removeZhao(zhao);
            } else {
                try {
                    if (!(texture instanceof cc.Texture2D)) {
                        var texture2d = new cc.Texture2D();
                        texture2d.initWithElement(texture);
                        texture2d.handleLoadedTexture();
                        texture = texture2d;
                    }
                    that.setHeadTexture(texture);
                }
                catch (e) {
                    dzmjNamespace.showDialog("1: e.name" + e.name + "-" + "e.message" + e.message + "-" + "e.message" + e.description);
                }

                that.removeZhao(zhao);
            }
        });
    },

    getIconName: function (iconID, offset) {
        var realName = (iconID - offset).toString();
        while (realName.length < 3) {
            realName = "0" + realName;
        }
        return realName;
    },

    loadComplete: function () {
        //加载远程图片
        var self = this;
        var url = this.headUrl;
        if (!URL_HEAD_LIST[url]) {
            URL_HEAD_LIST[url] = [];
            URL_HEAD_LIST[url].push(this);
            xinyou.log("xygame", "load head remote...");
            cc.loader.loadImg(url, {width: 100, height: 100}, function (err, texture) {
                xinyou.log("xygame", "load head remote get.");
                if (err) {
                    return xinyou.log("xygame", "load head error " + err);
                }
                if (texture instanceof cc.Texture2D == false) {
                    URL_HEAD_LIST[url] = null;
                    return;
                }
                /*if(self.setTexture){
                 xy.NativeExpand.debugLog("xygame", "set head "+self);
                 self.setTexture(texture);
                 }*/
                var list = URL_HEAD_LIST[url];
                for (var i = 0; i < list.length; i++) {
                    var cur = list[i];
                    if (cur.setHeadTexture) {
                        cur.setHeadTexture(texture);
                    }
                }
                cc.textureCache.removeTexture(texture);
                URL_HEAD_LIST[url] = null;
                xinyou.log("xygame", "load head remote done.");
            });
        } else {
            xinyou.log("xygame", "load head push " + this);
            URL_HEAD_LIST[url].push(this);
        }
    },

    setHeadTexture: function (texture) {
        if (texture) {
            xinyou.log("xygame", "set head " + this);
            this.initWithTexture(texture);
            if (this.m_Width > 0 && this.m_Height > 0) {
                this.setHeadWH(this.m_Width, this.m_Height);
            }
        }
    },

    setHeadData: function (headID, _w, _h) {
        _w = _w || 0;
        _h = _h || 0;
        if (this.m_headID == headID) return;
        xinyou.log("xygame", "set head data headID:" + headID + " width:" + _w + " height:" + _h);
        this.m_headID = headID;
        this.loadImg(headID);
        this.setHeadWH(_w, _h);
    },

    /**
     *  设置头像宽高
     * @param _w
     * @param _h
     */
    setHeadWH: function (_w, _h) {
//		 xinyou.log("xygame", "set scale width:"+_w+" height:"+_h);
        var _size = this.getContentSize();
        this.m_Width = _w;
        this.m_Height = _h;
        if (_size && _size.width > 0 && _size.height > 0) {
            var xsW = this.m_Width / _size.width;
            var xsH = this.m_Height / _size.height;
            if (xsW > 0 && xsH > 0) {
//				 xinyou.log("xygame", "set scale "+xsW+":"+xsH);
                this.setScale(xsW, xsH);
            }
        }

        this.anchorX = 0;
        this.anchorY = 0;
    },

    getheadID: function () {
        return this.m_headID;
    }

});
/**头像地址*/
var URL_HEAD_LIST = [];